package jp.co.piratealliance.pirateAllianceWeb.web.action.order;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import jp.co.piratealliance.pirateAllianceWeb.common.base.BaseAction;
import jp.co.piratealliance.pirateAllianceWeb.def.Constants;
import jp.co.piratealliance.pirateAllianceWeb.dto.order.ScheduleInfoDto;
import jp.co.piratealliance.pirateAllianceWeb.helper.CommonHelper;
import jp.co.piratealliance.pirateAllianceWeb.service.order.OrderService;


/**
 * copyright pirate alliance team 2014
 *
 * @author lunxing
 */

public class ScheduleAction extends BaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private OrderService orderService;

	private List<ScheduleInfoDto> scheduleInfoDtoList;
	
	private ScheduleInfoDto scheduleInfoDto;
	
	private String start_day;
	
	private String end_day;
	
	private String order_id;
	
	private String schedule_img;
	
	private Calendar cal = Calendar.getInstance();
	
	private SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");

	public String insertSchedule() throws ParseException
	{
		long betweenDays = 1;
		if (this.start_day != this.end_day) {
			Date start = this.sdf.parse(this.start_day);
	        Date end = this.sdf.parse(this.end_day);

	        this.cal.setTime(start);
	        long startTime = this.cal.getTimeInMillis();
	        
	        this.cal.setTime(end);
	        long endTime = this.cal.getTimeInMillis();
	        
	        betweenDays = (endTime - startTime) / (1000 * 3600 * 24);
		}
		
		String day = this.start_day;
		ScheduleInfoDto scheduleInfoDto = null;
		
		this.scheduleInfoDtoList = new ArrayList<ScheduleInfoDto>();
		
		for (long i = 0; i <= betweenDays; i++) {
			scheduleInfoDto = new ScheduleInfoDto();
			String schedule_id = CommonHelper.getSequence(Constants.TB_SCHEDULE);
			
			scheduleInfoDto.setOrder_id(this.order_id);
			scheduleInfoDto.setSchedule_id(schedule_id);
			scheduleInfoDto.setDescriptionx("");
			scheduleInfoDto.setDay(day);
			scheduleInfoDto.setSchedule_img(this.schedule_img);
			
			int count = this.orderService.insertSchedule(scheduleInfoDto);

			if (1 == count) {
				this.scheduleInfoDtoList.add(scheduleInfoDto);
			}
			
			Date date = this.sdf.parse(day);
			this.cal.setTime(date);
			this.cal.add(Calendar.DATE, 1);
			date = this.cal.getTime();
			
			day = this.sdf.format(date);
		}
		

		return SUCCESS;
	}

	public String updateSchedules() throws ParseException
	{		
		//スケジュールを削除
		this.orderService.deleteScheduleByOrderId(this.order_id);
		long betweenDays = 1;
		if (this.start_day != this.end_day) {
			Date start = this.sdf.parse(this.start_day);
	        Date end = this.sdf.parse(this.end_day);

	        this.cal.setTime(start);
	        long startTime = this.cal.getTimeInMillis();
	        
	        this.cal.setTime(end);
	        long endTime = this.cal.getTimeInMillis();
	        
	        betweenDays = (endTime - startTime) / (1000 * 3600 * 24);
		}
		
		String day = this.start_day;
		ScheduleInfoDto scheduleInfoDto = null;
		
		List<ScheduleInfoDto> list = new ArrayList<ScheduleInfoDto>();
		int inde = -1;
		for (long i = 0; i <= betweenDays; i++) {
			inde = -1;
			for (int j = 0; j < this.scheduleInfoDtoList.size(); j++) {
				String dayClient = this.scheduleInfoDtoList.get(j).getDay();
				if (true == day.equalsIgnoreCase(dayClient)) {
					inde = j;
					break;
				}
			}
			
			if (inde > -1) {
				scheduleInfoDto = this.scheduleInfoDtoList.get(inde);
			} else {
				scheduleInfoDto = new ScheduleInfoDto();
				String schedule_id = CommonHelper.getSequence(Constants.TB_SCHEDULE);
				
				scheduleInfoDto.setOrder_id(this.order_id);
				scheduleInfoDto.setSchedule_id(schedule_id);
				scheduleInfoDto.setDescriptionx("");
				scheduleInfoDto.setDay(day);
				scheduleInfoDto.setSchedule_img(this.schedule_img);
			}
			
			int count = this.orderService.insertSchedule(scheduleInfoDto);

			if (1 == count) {
				list.add(scheduleInfoDto);
			}
			
			Date date = this.sdf.parse(day);
			this.cal.setTime(date);
			this.cal.add(Calendar.DATE, 1);
			date = this.cal.getTime();
			
			day = this.sdf.format(date);
		}
		
		this.scheduleInfoDtoList.clear();
		this.scheduleInfoDtoList.addAll(list);
		
		return SUCCESS;
	}
	
	public String getScheduleByOrderId()
	{
		this.scheduleInfoDtoList = this.orderService.getScheduleByOrderId(this.order_id);
		return SUCCESS;
	}

	/**
	 * @return the orderService
	 */
	public OrderService getOrderService() {
		return orderService;
	}

	/**
	 * @param orderService the orderService to set
	 */
	public void setOrderService(OrderService orderService) {
		this.orderService = orderService;
	}

	/**
	 * @return the scheduleInfoDtoList
	 */
	public List<ScheduleInfoDto> getScheduleInfoDtoList() {
		return scheduleInfoDtoList;
	}

	/**
	 * @param scheduleInfoDtoList the scheduleInfoDtoList to set
	 */
	public void setScheduleInfoDtoList(List<ScheduleInfoDto> scheduleInfoDtoList) {
		this.scheduleInfoDtoList = scheduleInfoDtoList;
	}

	/**
	 * @return the start_day
	 */
	public String getStart_day() {
		return start_day;
	}

	/**
	 * @param start_day the start_day to set
	 */
	public void setStart_day(String start_day) {
		this.start_day = start_day;
	}

	/**
	 * @return the end_day
	 */
	public String getEnd_day() {
		return end_day;
	}

	/**
	 * @param end_day the end_day to set
	 */
	public void setEnd_day(String end_day) {
		this.end_day = end_day;
	}

	/**
	 * @return the order_id
	 */
	public String getOrder_id() {
		return order_id;
	}

	/**
	 * @param order_id the order_id to set
	 */
	public void setOrder_id(String order_id) {
		this.order_id = order_id;
	}

	/**
	 * @return the scheduleInfoDto
	 */
	public ScheduleInfoDto getScheduleInfoDto() {
		return scheduleInfoDto;
	}

	/**
	 * @param scheduleInfoDto the scheduleInfoDto to set
	 */
	public void setScheduleInfoDto(ScheduleInfoDto scheduleInfoDto) {
		this.scheduleInfoDto = scheduleInfoDto;
	}

	/**
	 * @return the schedule_img
	 */
	public String getSchedule_img() {
		return schedule_img;
	}

	/**
	 * @param schedule_img the schedule_img to set
	 */
	public void setSchedule_img(String schedule_img) {
		this.schedule_img = schedule_img;
	}

}
